package tournoi.stateless;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import modele.Admin;
import modele.Equipe;

public class VerifFunction {

	public static boolean equipeExist(EntityManager em, String nomEquipe) {
		Equipe e = null;
		Query q = em.createQuery("From Equipe e where e.nom=:nomEquipe");
		q.setParameter("nomEquipe", nomEquipe);
		try {
			e = (Equipe) q.getSingleResult();
		} catch (NoResultException nre) {
			return false;
		}
		return true;
	}
	
	public static boolean isAdmin(EntityManager em, String login, String password) {
		Admin admin = (Admin) em.find(Admin.class,
				login);
		
		if (admin==null){
			
			return false;
		}
		else{
			if (password.equals(admin.getPassword())){
				return true;
			}
			else return false;
		}
	}
	
	public static boolean verifTournoi(EntityManager em, String nom)
	{
		Query q = em.createQuery("From Tournoi t where t.nom=:nom");
		q.setParameter("nom", nom);
		try 
		{
			q.getSingleResult();
		} catch (NoResultException nre) {
			return true;
		}
		return false;
	}
	
	public static boolean verifArbitre(EntityManager em, String nom, String prenom)
	{
		Query q = em.createQuery("From Arbitre a where a.nom=?1 and a.prenom=?2");
		q.setParameter(1, nom);
		q.setParameter(2, prenom);
		try 
		{
			q.getSingleResult();
		} catch (NoResultException nre) {
			return true;
		}
		return false;
	}
	
}
